<?php

namespace app\admin\controller;

use app\admin\service\EchartsService;
use app\admin\service\ReportService;
use app\BaseController;
use app\model\ConsultOfflineResultModel;
use app\model\ConsultOnlineResultModel;
use app\model\group_activity\GroupActivityTaskUserResultModel;
use app\model\ProxyEvaluationResultModel;
use app\model\school\SchoolCourseChapterResultModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultContinuityModel;
use app\model\TaskUserResultModel;
use app\model\training\TrainingTaskUserResultModel;
use app\model\training\TrainingTypeModel;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\WaitGroup;
use think\App;
use think\facade\Log;
use think\facade\View;

class Echarts extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function self(): string
    {
        $id = $this->request->param('id');
        $ReportService = new ReportService($this->app);
        $data = $ReportService->getSelfReportInfo($id);
        View::assign('data', $data);
        return View::fetch('self');
    }

    /**
     * 生成word团体报告
     * @return bool|string
     */
    public function GroupReport(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $EchartsService = new EchartsService($this->app);
            return success($EchartsService->GroupReport($data));
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word自我报告
     * @return bool|string
     */
    public function SelfWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $info = $SelfEvaluationResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word任务报告
     * @return bool|string
     */
    public function TaskWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $TaskUserResultModel = new TaskUserResultModel();
            $info = $TaskUserResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word任务连评报告
     * @return bool|string
     */
    public function ContinuityWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $TaskUserResultContinuityModel = new TaskUserResultContinuityModel();
            $info = $TaskUserResultContinuityModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word他人代测报告
     * @return bool|string
     */
    public function ProxyWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $info = $ProxyEvaluationResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word线上咨询量表测评报告
     * @return bool|string
     */
    public function ConsultOnlineWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $ConsultOnlineResultModel = new ConsultOnlineResultModel();
            $info = $ConsultOnlineResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word线下预约量表测评报告
     * @return bool|string
     */
    public function ConsultOfflineWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $ConsultOfflineResultModel = new ConsultOfflineResultModel();
            $info = $ConsultOfflineResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word-团体活动
     * @return bool|string
     */
    public function GroupActivityWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $GroupActivityTaskUserResultModel = new GroupActivityTaskUserResultModel();
            $info = $GroupActivityTaskUserResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word-心理学堂
     * @return bool|string
     */
    public function SchoolWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $SchoolCourseChapterResultModel = new SchoolCourseChapterResultModel();
            $info = $SchoolCourseChapterResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 生成word-培训讲座
     * @return bool|string
     */
    public function TrainingWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $TrainingTaskUserResultModel = new TrainingTaskUserResultModel();
            $info = $TrainingTaskUserResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $SelfWord = $EchartsService->SelfWord($data);
            $HospitalWord = $EchartsService->HospitalWord($data);
            $info->save([
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
                'concise_word' => $HospitalWord['word'],
                'concise_pdf' => $HospitalWord['pdf'],
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}